package com.kyzh.core.pager.weal.groupchat

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.Nav
import com.gushenge.core.beans.TuiQunBean
import com.kingja.loadsir.core.LoadService
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.databinding.ActGroupchatBinding
import com.kyzh.core.databinding.ItemGroupchat1Binding
import com.kyzh.core.pager.weal.groupchat.vm.GroupChatVM
import com.kyzh.core.utils.loadServiceInit
import com.kyzh.core.utils.navClickByLogin
import com.kyzh.core.utils.showLoading
import org.jetbrains.anko.support.v4.onRefresh

class GroupChatActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, GroupChatActivity::class.java)
            context.startActivity(starter)
        }
    }
    private val adapter = Adapter()
    private val vm by viewModels<GroupChatVM> ()
    private lateinit var db :ActGroupchatBinding
    private lateinit var loadsir: LoadService<Any>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this, R.layout.act_groupchat)
        db.inrev.rev.adapter = adapter
        loadsir = loadServiceInit(db.inrev.rev) {
            //点击重试时触发的操作
            loadsir.showLoading()
        }
        vm.getGroupChatList {
            loadsir.showSuccess()
            adapter.setNewInstance(this)
        }

        db.inrev.swipe.onRefresh {
            vm.getGroupChatList {
                loadsir.showSuccess()
                db.inrev.swipe.isRefreshing = false
                adapter.setNewInstance(this)
            }
        }
        db.searchClose.setOnClickListener {
            finish()
        }

    }
    private inner class Adapter: BaseQuickAdapter<TuiQunBean,BaseDataBindingHolder<ItemGroupchat1Binding>>(R.layout.item_groupchat1){
        override fun convert(
            holder: BaseDataBindingHolder<ItemGroupchat1Binding>,
            item: TuiQunBean
        ) {
           holder.dataBinding?.data = item
            holder.dataBinding?.root?.setOnClickListener {
                context.navClickByLogin(Nav(login = 1,url =item.url ))
            }
        }
    }
}